#pragma once

#include "stdafx.h"
#include "FilterManager.h"

#include <unordered_map>
#include <vector>
#include <string>
#include <utility> //for "pair"

using namespace std;
using namespace ATL;

class CAdvertBlocker
{
public:
	CAdvertBlocker(void);
	~CAdvertBlocker(void);

	void LoadResources();

	void Initialize(CComPtr<IWebBrowser2> webBrowser);

	void CleanUpAfterInitialization();

	void RemoveAdverts(CComPtr<IWebBrowser2> webBrowser);

	void LoadFilterManagerFromAppDataFolder();

	//test method for creating serialized FilterManager, not used.
	void CreateFilterManagerAndSerializeToFile(string pathToBlockingRules, string pathToSerializedFileMgr);

private:
	typedef unordered_map<wstring, vector<CComPtr<IHTMLElement>>> ClassElementsMap;
	ClassElementsMap _elementsOfClass;
	typedef  bool (CAdvertBlocker::*FilterFunction)(CComPtr<IHTMLElement> e);

	CFilterManager _filterManager;

	void CreateClassElementsMap(CComPtr<IHTMLDocument2> document);

	void RemoveAdverts(CComPtr<IHTMLDocument3> document3);
	void RemoveAdvertsBasedOnElementHidingRules(CComPtr<IHTMLDocument3> document3);
	void RemoveHtmlElementWithId(CComPtr<IHTMLDocument6> document, const wstring& id);
	void RemoveHtmlElementWithClass(CComPtr<IHTMLDocument6> document, const wstring& cl);

	void RemoveObjectsAdverts(CComPtr<IHTMLDocument3> document3);
	bool IsObjectAdvert(CComPtr<IHTMLElement> element);
	wstring GetSrcFromObjectElement(CComPtr<IHTMLElement> object);
	bool IsParamTagWithNameMovieAttribute(CComPtr<IHTMLDOMNode> htmlNode);
	bool IsParamTag(CComPtr<IHTMLDOMNode> node);
	bool HasNameAttributeWithMovieValue(CComPtr<IHTMLDOMNode> node);
	wstring GetValueAttribute(CComPtr<IHTMLDOMNode> param);
	
	void RemoveAdvertsWithSrcAttribute(CComPtr<IHTMLDocument3> document3, CComBSTR elementName);
	void RemoveHtmlElements(CComPtr<IHTMLElementCollection> elements,  FilterFunction isAdvert);
	void RemoveHtmlDOMNode(CComPtr<IHTMLDOMNode> node);
	void RemoveHtmlElement(CComPtr<IHTMLElement> element);	

	bool IsSrcAttributeAnAdvert(CComPtr<IHTMLElement> element);
};

